import { defineStore } from "pinia";
import { LocalStorage, UtilTool } from "@webxue/common";
import useMapping from "../utils/mapping.util";

export const webxDictStore = defineStore("webxDict", {
  state:  () => ({
    // 上次更新时间
    updateDate:(LocalStorage.get(useMapping().LocalKeys.dict)?.updateDate || "1970-01-01 01:01:00") as string,
    // 当前所有的字典数据
    dataSource:(LocalStorage.get(useMapping().LocalKeys.dict)?.dataSource || {}) as Record<string, any>
  }),
  actions:{
    // 设置更新时间
    setUpdateDate(){
      this.updateDate = UtilTool.dayjs().format("YYYY-MM-DD HH:mm:ss");
      const dictConfig = LocalStorage.get(useMapping().LocalKeys.dict) || {};
      dictConfig.updateDate = this.updateDate;
      LocalStorage.set(useMapping().LocalKeys.dict, dictConfig);
    },
    // 设置所有字典数据
    setDataSource(dataSource:Record<string, any>){
      this.dataSource = dataSource;
      const dictConfig = LocalStorage.get(useMapping().LocalKeys.dict) || {};
      dictConfig.dataSource = dataSource;
      LocalStorage.set(useMapping().LocalKeys.dict, dictConfig);
    }
  }
})